
var WindData = new Object();

WindData.execute = function(command, data) {
	var result = null;
	if(command=="getTypes") {
		result = WindData.getTypes();
	} else if(command=="getTasks") {
		result = getTasks();
	} else if(command=="getTask") {
		result = getTask(data);
	} else if(command=="insertTask") {
		result = insertTask(data);
	} else if(command=="updateTask") {
		result = updateTask(data);
	} else {
		var request = "json=" + Object.toJSON(command);
		var response = WindAjax.send(request);
		try {
			result = eval(response);
		} catch (e) {
			alert("Error [" + e + "] converting string to json: \n" + response)
		}
		
	}
	return result;
}

WindData.createCommand = function(operation, type, sort, filterProperty, filterOperator, filterValue) {
	var cmd = new WindDataCommand();
	cmd.setOperation(operation);
	cmd.setType(type);
	if(sort!=null && sort.length>0) {
		cmd.setSort(sort);
	}
	if(filterProperty!=null && filterProperty.length>0) {
		cmd.addFilter(filterProperty, filterOperator, filterValue);
	}
	return cmd;
}

var tasks = null;	

WindData.getTypes = function() {
	var result = eval(WindAjax.send("getTypes"));
	return result;
}

WindData.getView = function() {
	var result = eval(WindAjax.send("getView"));
	return result;
}

WindData.createNewIdForType = function(type) {
	
}

WindData.getTypeFromId = function(id) {
	
}

function createTasks() {
	tasks = new TasksDTO();

	var task1 = new TaskDTO;
	{
		var startDate = new Date();
		
		var year = 2011;
		var month = 4;
		var day = 11;
		var hour = 10;
		var minute = 20;
		var duration = 240+30+20;
		var blue = "#9999ff";
		
		startDate.setYear(year);
		startDate.setMonth(month-1);
		startDate.setDate(day+1);
		startDate.setHours(hour);
		startDate.setMinutes(minute);
		startDate.setSeconds(0,0);
		task1.setId("task1");
		task1.setTitle("task1");
		task1.setDescription("Lorem ipsum dolor");
		task1.setStartDate(startDate);
		task1.setColor(blue);
		task1.setDuration(duration);
		tasks.addTask(task1);
	}

	var task2 = new TaskDTO;
	{
		task2.setId("task2");
		task2.setTitle("task2");
		task2.setPreviousTask(task1);
		task2.setColor("#ff9999");
		task2.setDuration(89);
		tasks.addTask(task2);
	}
	
	var task3 = new TaskDTO;
	{
		task3.setId("task3");
		task3.setTitle("task3");
		task3.setPreviousTask(task2);
		task3.setColor("#ff99ff");
		task3.setDuration(168);
		tasks.addTask(task3);
	}

	var task4 = new TaskDTO();
	{
		var startDate = new Date();
		
		var year = 2011;
		var month = 4;
		var day = 11;
		var hour = 8;
		var minute = 0;
		var duration = 60*30-12;
		var green = "#99ff99";
		
		startDate.setYear(year);
		startDate.setMonth(month-1);
		startDate.setDate(day+1);
		startDate.setHours(hour);
		startDate.setMinutes(minute);
		startDate.setSeconds(0,0);
		
		task4.setId("task4");
		task4.setTitle("task4");
		task4.setStartDate(startDate);
		task4.setColor(green);
		task4.setDuration(duration);
		tasks.addTask(task4);
	}
	
	var task5 = new TaskDTO;
	{
		task5.setId("task5");
		task5.setTitle("task5");
		task5.setColor("#ff99ff");
		task5.setDuration(168);
		task3.addTask(task5);
	}
}

function getTasks() {
	if(tasks==null) {
		createTasks();
	}
	return tasks;
}

function getTask(taskId) {
	var task = new TaskDTO;
	task.setId("task100");
	task.setTitle("task5");
	task.setColor("#ff99ff");
	task.setDescription("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.");
	task.setDuration(168);
	return task;
}

insertTask = function(task) {
	tasks.addTask(task);
}

updateTask = function(task) {
	//tasks.addTask(task);
}

